<?php
require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

?>


<div id="subcontent">
<h4>View Opponent Info</h4>

<form>
	
<select class="form-control"  name="season" id="select3">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM seasons";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}


?>
</select>

<?php

if (isset($_POST['season'])) {
  
  $setseason = $_POST['season'];

?>
<br>
<select class="form-control" name="team" id="select2">

<?php

if (isset($_POST['team'])) {

	$setteam = $_POST['team'];

	echo "<option value=\"$setteam\" selected=\"selected\">$setteam is selected</option>";

} else {         	
	echo "<option value=\"\" disabled=\"disabled\" selected=\"selected\">Select a team</option>";
}

$sql3 = "SELECT teamname
  FROM opponents
  WHERE season = '$setseason'";

$result3=$db->query($sql3);

while($row3 = $result3->fetch_assoc()){	
    $teamname = $row3['teamname']; 
    echo "<option value=\"$teamname\">$teamname</option>";
	} // Close while

?>

 </select>
 <br>

<?php } //close POST season ?>

 <?php 

if (isset($_POST['team']) && isset($_POST['season'])) {

$setteam = $_POST['team'];
$setseason = $_POST['season'];

 $sql4 = "SELECT text
  FROM opponents
  WHERE season = '$setseason' AND teamname = '$setteam'";


  	$result4=$db->query($sql4);
	$row4=$result4->fetch_assoc();
  	$text=$row4['text'];   	
  	if ($text != ''){
   		echo $text;
   	} else { echo "no info available"; }

?>
<br><br>
<button class="form-control btn btn-success btn-sm" id="submit" type="submit">Edit</button>

<?php } ?>

</form>
</div>

<script type="text/javascript">
	
$("#select3").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./opponents_view.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#subcontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#subcontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#select2").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./opponents_view.php",
        data: postData,
        success: function(data) { 
        	//(postData);
            $("#subcontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#subcontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#submit").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./opponents_edit.php",
        data: postData,
        success: function(data) { 
        	alert(postData);
            $("#subcontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#subcontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>